package HashTablePackage;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.engine.discovery.predicates.IsNestedTestClass;

/**
 * @author Lzm
 * @version 1.0
 */
public class HashTable {
  @Test
  public void LinkedListTest(){
    HashTa hashta = new HashTa(7);
    Emp e1 = new Emp(1,"1");
    Emp e2 = new Emp(2,"2");
    Emp e3 = new Emp(3,"3");
    Emp e4 = new Emp(4,"4");
    Emp e5 = new Emp(5,"5");
    Emp e6 = new Emp(6,"6");
    Emp e7 = new Emp(7,"7");
    Emp e8 = new Emp(8,"8");
    hashta.add(e1);
    hashta.add(e2);
    hashta.add(e3);
    hashta.add(e4);
    hashta.add(e5);
    hashta.add(e6);
    hashta.add(e7);
    hashta.add(e8);

    hashta.list();
    hashta.findEmpById(4 );
    hashta.delete(4);
    hashta.list();

    EmpLinkedList empLinkedList = new EmpLinkedList();
    empLinkedList.add(e1);
    empLinkedList.add(e2);
    empLinkedList.add(e3);
    empLinkedList.add(e4);
    empLinkedList.add(e5);

    empLinkedList.delete(1);
    empLinkedList.delete(5);
    empLinkedList.list(1);
  }
}
class Emp{
  public int id;
  public String name;
  public Emp next;
  public Emp(){

  }
  public Emp(int id,String name){
    this.id = id;
    this.name = name;
  }

  @Override
  public String toString() {
    return "Emp{" +
      "id=" + id +
      ", name='" + name + '\'' +
      '}';
  }
}
class EmpLinkedList{
  public Emp head;

  /*添加员工*/
  public void add(Emp emp){
    if (head == null){
      head = emp;
      return;
    }
    Emp temp = head;
    while(true){
      if (temp.next == null){
        break;
      }
      temp = temp.next;
    }
    temp.next = emp;
  }
  /*删除员工*/
  public void delete(int id){
    if (head == null){
      System.out.println("链表为空");
      return ;
    }

    Emp temp = head;
    while(temp.next != null){
      if (head.id == id){
        head = head.next;
      }
      if (temp.next.id == id){
        temp.next = temp.next.next;
        return ;
      }
      temp = temp.next;
    }
    return ;
  }
  /*遍历链表*/
  public void list(int no){
    if (head == null){
      System.out.println("第" + (no + 1) + "条链表为空 ");
      return;
    }
    Emp temp = head;
    System.out.println("第" + (no + 1) + "条链表的节点为");
    while(true){
      System.out.println(temp);
      if (temp.next == null){
        break;
      }
      temp = temp.next;
    }
  }
  /*查找节点*/
  public Emp findEmpById(int id){
    if (head == null){
      System.out.println("链表为空");
      return null;
    }
    Emp temp = head;
    while(true){
      if (temp.id == id){
        return temp;
      }
      if (temp.next == null){
        break;
      }
      temp = temp.next;
    }
    return null;
  }
}
class HashTa{
  private EmpLinkedList[] empLinkedList;
  private int size; // 表示哈希表内链表的条数
  public HashTa(int size){
    this.size = size;
    this.empLinkedList = new EmpLinkedList[size];
    /*注意, 构造器虽然将哈希表创建出来, 但是哈希表中的每条链表也需要进行初始化*/
    for(int i = 0 ; i < size ; i ++){
      empLinkedList[i] = new EmpLinkedList();
    }
  }
  /*添加员工, 根据要添加员工的id找到要添加的链表*/
  public void add(Emp emp){
    if (emp != null){
      empLinkedList[getHash(emp)].add(emp);
    }
  }
  /*遍历哈希表*/
  public void list(){
    for(int i = 0 ; i < size ; i++){
      empLinkedList[i].list(i);
    }
  }
  /*散列函数*/
  public int getHash(Emp emp){
    return emp.id % this.size;
  }
  /*查找节点*/
  public Emp findEmpById(int id){
    if (size == 0){
      return null;
    }
    Emp emp = empLinkedList[id % this.size].findEmpById(id);
    if (emp != null){
      System.out.println("在第" + (id % this.size + 1) + "找到了");
    }
    else{
      System.out.println("没找到");
    }
    return emp;
  }
  /*删除节点*/
  public void delete(int id){
    empLinkedList[id % this.size].delete(id);
  }
}
